﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Owl.Contracts;
using System.Data.Objects;
using System.Diagnostics;
using Owl.Questionnarie.Entities;

namespace Owl.Questionnarie.DAL
{
    public class SubjectDAL: ICrud<Subjects, Guid>
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Subjects GetById(Guid key)
        {
            Trace.WriteLine("SubjectDAL", "Iniciando GetById");
            using (QuestionnarieEntities context = new QuestionnarieEntities())
            {
                try
                {
                    Subjects s = (from subjects in context.Subjects
                                 where subjects.SubjectId == key
                                 select subjects).FirstOrDefault();

                    Trace.WriteLine("SubjectDAL", "Retornando objeto");
                    return s;
                }

                catch (Exception ex)
                {
                    Trace.WriteLine("SubjectDAL", ex.Message);
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public List<Subjects> GetAll()
        {
            Trace.WriteLine("SubjectDAL", "Iniciando GetAll");
            using (QuestionnarieEntities context = new QuestionnarieEntities())
            {
                try
                {
                    List<Subjects> s = (from subjects in context.Subjects
                                        select subjects).ToList();

                    Trace.WriteLine("SubjectDAL", "Retornando objeto");
                    return s;
                }

                catch (Exception ex)
                {
                    Trace.WriteLine("SubjectDAL", ex.Message);
                    throw ex;
                }
            }

        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public Subjects Insert(Subjects entity)
        {
            Trace.WriteLine("SubjectDAL", "Iniciando insert");
            using (QuestionnarieEntities context = new QuestionnarieEntities())
            {
                try
                {
                    entity.SubjectId = Guid.NewGuid();
                    context.AddToSubjects(entity);
                    context.SaveChanges();

                    Trace.WriteLine("SubjectDAL", "Inserção realizada");

                    Trace.WriteLine("SubjectDAL", "Retornando objeto");
                    return entity = (from subjects in context.Subjects
                                     where subjects.SubjectId == entity.SubjectId
                                     select subjects).FirstOrDefault();

                }

                catch (Exception ex)
                {
                    Trace.WriteLine(ex.Message, "SubjectDAL");
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entity"></param>
        public void Update(Subjects entity)
        {
            Trace.WriteLine("SubjectDAL", "Iniciando update");
            using (QuestionnarieEntities context = new QuestionnarieEntities())
            {
                try
                {
                    Subjects s = (from subjects in context.Subjects
                                  where subjects.SubjectId == entity.SubjectId
                                  select subjects).FirstOrDefault();


                    s.Description = entity.Description;
                    context.SaveChanges();
                    Trace.WriteLine("SubjectDAL", "Alterações salvas");
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("SubjectDAL", ex.Message);
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        public void Delete(Guid key)
        {
            Trace.WriteLine("SubjectDAL", "Iniciando delete");
            using (QuestionnarieEntities context = new QuestionnarieEntities())
            {
                try
                {
                    Subjects s = (from subjects in context.Subjects
                                  where subjects.SubjectId == key
                                  select subjects).FirstOrDefault();

                    context.DeleteObject(s);
                    context.SaveChanges();
                    Trace.WriteLine("SubjectDAL", "Deleção realizada");
                }
                catch (Exception ex)
                {
                    Trace.WriteLine("SubjectDAL", ex.Message);
                    throw ex;
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public List<Subjects> GetByString(string text)
        {
            Trace.WriteLine("SubjectDAL", "Iniciando GetByString");
            using (QuestionnarieEntities context = new QuestionnarieEntities())
            {
                try
                {
                    List<Subjects> s = context.Subjects.Where("it.Description LIKE @searchTerm", 
                                                            new ObjectParameter("searchTerm", text)).ToList();

                    Trace.WriteLine("SubjectDAL", "Retornando objeto");
                    return s;
                }

                catch (Exception ex)
                {
                    Trace.WriteLine("SubjectDAL", ex.Message);
                    throw ex;
                }
            }
        }
    }
}
